//
// Created by kevin on 2024/11/27.
//

#include "lte_getresourcegrid.h"

void lte_getresourcegrid(complex* sig_time, unsigned int Nfft, unsigned int *CP,unsigned int CP_len, unsigned int NDLRB){
    //% should be a positive value
    int  delta_shift_time = 2;

    //double fft_in[14]= {0};
    complex* fft_buf = new complex[Nfft];
    complex* fft_out = new complex[Nfft];
    int start_idx = 0;
    for(int i = 0; i < 14; i++){
        int nfft_tmp = Nfft*i;   //only normal cp is supported;
        int cp_tmp = 0;
        for(int j = 0; j < i; j++){
            cp_tmp += CP[j];
        }
        start_idx = nfft_tmp + cp_tmp + 1 - delta_shift_time;
        int start = start_idx;


        complex*fft_in = &sig_time[start];
        int fft_len = Nfft;
        //debug used
        memcpy(fft_buf, fft_in, sizeof(complex)*fft_len);
        fft_out = fft(fft_in, fft_len);
        fft_out = fftshift(fft_out);

        complex* resourcegrid_tmp = fft_out;
        //remove DC
        // index of first subcarrier in IFFT input
        int firstSC = (Nfft/2) - (NDLRB*12/2) + 1;
        int len1 = firstSC+NDLRB*12/2-1;
        complex* pstart = &resourcegrid_tmp[firstSC];
        complex* pout ;
        memcpy(pout, pstart, len1*sizeof(comlplex));
        int start2 = firstSC+NDLRB*12/2+1;
        int len2 = NDLRB*12/2-1;
        complex* pout2 = pout + len1 ;
        complex* pstart2 =&resourcegrid_tmp[start2];
        memory(pout2, pstart2, sizeof(complex)*len2);
        //resourcegrid = resourcegrid_tmp([firstSC:firstSC+NDLRB*12/2-1 ...
        //firstSC+NDLRB*12/2+1+(0:NDLRB*12/2-1)],:);
    }
}